<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-08-31 16:06:30
 * @Description: 扫描设备
 */
class ScsScanningController extends BaseController {


    /**
     * @Author:      lyt
     * @DateTime:  2017-08-31 16:08:02
     * @Description: 参看当天的还未未离店的扫描设备
     */
    public function scanning(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $slp=ScanningLicensePlates::leftJoin("scanning_plates_orders","scanning_plates_orders.plate_id","=","scanning_license_plates.id")->where("scanning_license_plates.collecting_shop",$sc_user->shop_id)->where("scanning_license_plates.shop_status",0)->where("scanning_license_plates.created_at",">",date("Y-m-d 00:00:00",time()))->where("scanning_license_plates.created_at","<",date("Y-m-d 23:59:59",time()))->whereNull("scanning_plates_orders.id")->select("scanning_license_plates.*")->orderBy("scanning_license_plates.created_at","desc")->get();
            $finally_arr=array();
            foreach ($slp as $_slp) {
                $finally_arr[]=array(
                    "id"=>$_slp->id,
                    "scanning_plate"=>$_slp->scanning_plate,
                    "used_plate"=>$_slp->used_plate,
                    "collecting_shop"=>$_slp->collecting_shop,
                    "big_picture"=>Config::get('wentong.scanning_img_url').$_slp->big_picture,
                    "small_picture"=>Config::get('wentong.scanning_img_url').$_slp->small_picture,
                    "shop_status"=>$_slp->shop_status,
                    "created_at"=>$_slp->created_at."",
                );
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($finally_arr),"scanning"=>$finally_arr));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "company", "event" => "scanning", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }    



    /**
     * @Author:      lyt
     * @DateTime:  2017-08-31 16:08:02
     * @Description: 门店的扫描照片离店
     */
    public function scanningDeparture(){
        $validator = Validator::make(Input::all(),
            array(
                'token'=>'required',
                "id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $slp=ScanningLicensePlates::find($id);
            if($slp->collecting_shop!=$sc_user->shop_id){
                $response = Lang::get("scs_error.scanning_error_31014");
                break;
            }
            $slp->shop_status=1;
            $slp->save();     
            $result = array(
                'code' => 1,
                'type' => 2,        
                'shop_id' => $sc_user->shop_id,         
                'order_id' => "",
                'plate_car_no' =>$slp->scanning_plate,
                'plate_id' =>$slp->id,
            ); 
            RedisHelper::push($result,"anhui_wash_panel");      
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$id));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "company", "event" => "scanningDeparture", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }    
}